package com.shoutao.website.controller.datacontroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shoutao.website.contact.*;
import com.shoutao.website.entity.Blogcommentinfo;
import com.shoutao.website.entity.Bloginfo;
import com.shoutao.website.mapper.BlogcommentinfoMapper;
import com.shoutao.website.mapper.BloginfoMapper;
import com.shoutao.website.utils.ObjectUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cqin on 2020/10/30.
 */
@RestController
@RequestMapping(value = "/api/blog")
public class BlogController {

    @Autowired
    private BloginfoMapper bloginfoMapper;

    @Autowired
    private BlogcommentinfoMapper blogcommentinfoMapper;

    @ApiOperation(value = "blog查询接口", notes = "blog查询接口")
    @RequestMapping(value = "/searchBlog.do", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", dataType = "SearchBlogRequest", required = true)
    })
    public SearchBlogResponse searchProduct(HttpServletRequest httpServletRequest, SearchBlogRequest request) throws SQLException {

        SearchBlogResponse response = new SearchBlogResponse();
        List<Bloginfo> resultList = new ArrayList<>();

        List<Bloginfo> bloginfoList = bloginfoMapper.selectList(null);

        if (ObjectUtils.isNullOrEmpty(request.getBlogdesc())) {
            response.setBloginfoList(bloginfoList);
        } else {
            for (Bloginfo bloginfo : bloginfoList) {
                if (bloginfo.getTitle().contains(request.getBlogdesc())
                        || bloginfo.getContent().contains(request.getBlogdesc())) {
                    resultList.add(bloginfo);
                }
            }
            response.setBloginfoList(resultList);
        }
        return response;
    }



    @ApiOperation(value = "博客详情接口", notes = "博客详情接口")
    @RequestMapping(value = "/showBlogDetail.do", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", dataType = "ShowBlogDetailRequest", required = true)
    })
    public ShowBlogDetailResponse showProductDetail(HttpServletRequest httpServletRequest, ShowBlogDetailRequest request) throws SQLException {

        ShowBlogDetailResponse response = new ShowBlogDetailResponse();

        // 基础信息
        Bloginfo bloginfo = bloginfoMapper.selectById(request.getBlogId());
        response.setBloginfo(bloginfo);


        // 评论
        Blogcommentinfo blogcommentinfoSample= new Blogcommentinfo();
        blogcommentinfoSample.setBlogid(request.getBlogId());
        QueryWrapper<Blogcommentinfo> wapper = new QueryWrapper<>(blogcommentinfoSample);
        List<Blogcommentinfo> blogcommentList = blogcommentinfoMapper.selectList(wapper);
        response.setBlogcommentinfoList(blogcommentList);

        return response;
    }


    @ApiOperation(value = "保存博客", notes = "保存博客")
    @RequestMapping(value = "/saveBlog.do", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", dataType = "ShowBlogDetailRequest", required = true)
    })
    public SaveBlogResponse saveBlog(HttpServletRequest httpServletRequest, SaveBlogRequest request) throws SQLException {

        SaveBlogResponse response = new SaveBlogResponse();

        HttpSession httpSession = httpServletRequest.getSession();

        if(httpSession == null || httpSession.getAttribute("user") == null
                ||!"admin".equals((String)httpSession.getAttribute("user"))) {
            response.setResult(false);
            response.setResultMessage("没有足够的权限");
            return response;
        }

        if(request.getBlogId() ==0){
            // 新增
            Bloginfo bloginfo = new Bloginfo();
            bloginfo.setTitle(request.getTitle());
            bloginfo.setContent(request.getContent());
            bloginfo.setTags(request.getTags());

            bloginfo.setUsername((String)httpSession.getAttribute("user"));
            bloginfo.setDatachange_createtime(new Timestamp(System.currentTimeMillis()));

            bloginfoMapper.insert(bloginfo);

            response.setResult(true);
            response.setResultCode("SUCCESS");
            response.setResultMessage("添加成功");
        }

        return response;
    }



}
